package yahtzee;

import java.util.*;
import java.io.*;
/*
 * Problem ID: 110208
 * Algorithm: Let's attempt another backtracking solution!
 */
public class YahtzeeGame 
{
	public static void main(String [] args)
	{
		new YahtzeeGame().start();
	}
	
	public void start()
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		
		try
		{
			String [] diceRolls = new String[14];
			int [] scoreCard;
			int [] solutionArray;
			ArrayList<String> scoreTracker;
			
			int roundCounter = 1;
			String roundResults = "";
				
			while ((roundResults = reader.readLine()) != null)
			{
				diceRolls[roundCounter] = sortString(roundResults);
				
				roundCounter++;
				
				if (roundCounter == 14)
				{
					solutionArray = new int[14];
					
					for (int i = 1; i < solutionArray.length; i++)
					{
						solutionArray[i] = -1;
					}
					
					scoreCard = new int[14];
					scoreTracker = new ArrayList<String>();
					
					// Perform backtrack.
					backtrack(0, solutionArray, scoreCard, diceRolls, scoreTracker, 0);
					
					roundCounter = 1;
					diceRolls = new String[14];
				}
			}
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void backtrack(int k, int [] solutionArray, int [] scoreCard, String [] diceRolls, ArrayList<String> scoreTracker, int currentScore)
	{
		int [] candidates = new int[13];
		int numOfCandidates;
		
		if (isSolution(k, solutionArray, scoreCard, currentScore))
		{
			storeResults(solutionArray, scoreCard,scoreTracker, currentScore);
		}
		else
		{
			k++;
			
			numOfCandidates = constructCandidates(k, candidates, scoreCard, diceRolls[k], currentScore);
			
			for (int i = 0; i < numOfCandidates; i++)
			{
				solutionArray[k] = candidates[i];
				
				currentScore = makeMove(solutionArray[k], diceRolls[k], scoreCard, currentScore);
				backtrack(k, solutionArray, scoreCard, diceRolls, scoreTracker, currentScore);
				currentScore = unMakeMove(solutionArray[k], scoreCard, currentScore);
				
				solutionArray[k] = -1;
			}
		}
	}
	
	public int unMakeMove(int cardType, int [] scoreCard, int score)
	{
		score -= scoreCard[cardType];
		scoreCard[cardType] = 0;
		
		return score;
	}
	
	public int makeMove(int cardType, String diceRolled, int [] scoreCard, int score)
	{
		int sum = 0;
		int [] results = search(diceRolled);
		
		int partialSum = 0;
		
		if (cardType == 0)
		{
			return score;
		}
		
		for (int i = 1; i < results.length; i++)
		{
			partialSum += results[i] * i;
		}
		
		switch (cardType)
		{
			// sum of all ones.
			case 1:
				sum += results[1];
				break;
			// sum of all twos.
			case 2:
				sum += results[2] * 2;
				break;
			// sum of all threes.
			case 3:
				sum += results[3] * 3;
				break;
			// sum of all fours.
			case 4:
				sum += results[4] * 4;
				break;
			// sum of all fives.
			case 5:
				sum += results[5] * 5;
				break;
			// sum of all sixes.
			case 6: 
				sum += results[6] * 6;
				break;
			// Chance
			case 7:
				sum += partialSum;
				break;
			// three of kind.
			case 8:
				sum += partialSum;
				break;
			// four of kind.
			case 9:
				sum += partialSum;
				break;
			// Five of kind.
			case 10:
				sum += partialSum;
				break;
			// short straight.
			case 11:
				sum += 25;
				break;
			// long straight.
			case 12:
				sum += 35;
				break;
			// full house.
			case 13:
				sum += 40;
				break;
		}
		
		// Add it to scoreCard. 
		scoreCard[cardType] = sum;
		
		// Add it to totalScore.
		score += sum;
		
		return score;
	}
	
	public int constructCandidates(int k, int [] candidates, int [] scoreCard, String roll, int currentScore)
	{		
		int [] results = search(roll);
		int indexPosition = 0;
		
		indexPosition = evaluateFirstSixCards(candidates, scoreCard, results, currentScore, indexPosition);
		indexPosition = evaluateKinds(candidates, scoreCard, results, currentScore, indexPosition);
		indexPosition = evaluateStraight(candidates, scoreCard, results, currentScore, indexPosition);
		indexPosition = evaluateFullHouse(candidates, scoreCard, results, currentScore, indexPosition);
		
		// Check if chance is used.
		if (scoreCard[7] == 0)
		{
			candidates[indexPosition] = 7;
			indexPosition++;
		}
		
		if (indexPosition == 0)
		{
			return 1;
		}
		
		return indexPosition;
	}
	
	public int evaluateFirstSixCards(int [] candidates, int [] scoreCard, int [] results, int currentScore, int indexPosition)
	{
		for (int i = 1; i < results.length; i++)
		{
			if (results[i] != 0 && scoreCard[i] == 0)
			{
				candidates[indexPosition] = i;
				indexPosition++;
			}
		}
		
		return indexPosition;
	}
	
	public int evaluateKinds(int [] candidate, int [] scoreCard, int [] results, int currentScore, int indexPosition)
	{	
		for (int i = 1; i < results.length; i++)
		{
			switch (results[i])
			{
			case 3:
				if (scoreCard[8] == 0) 
				{
					candidate[indexPosition] = 8;
					indexPosition++;
				}
				break;
			case 4:
				if (scoreCard[9] == 0) 
				{
					candidate[indexPosition] = 9;
					indexPosition++;
				}
				break;
			case 5:
				if (scoreCard[10] == 0) 
				{
					candidate[indexPosition] = 10;
					indexPosition++;
				}
				break;
			}
		}
		
		return indexPosition;
	}
	
	public int evaluateStraight(int [] candidate, int [] scoreCard, int [] results, int currentScore, int indexPosition)
	{
		boolean isLongStraight = false;
		boolean isShortStraight = false;
		int counter = 0;
		
		// Check for long straight.
		for (int i = 1; i < results.length; i++)
		{
			if (results[i] == 1)
				counter++;
			else
				counter = 0;
			
			if (counter == 4)
				isShortStraight = true;
			
			if (counter == 5)
				isLongStraight = true;
		}
		
		if (isShortStraight == true)
		{
			if (scoreCard[11] == 0)
			{
				candidate[indexPosition] = 11;
				indexPosition++;
			}
		}
		
		if (isLongStraight == true)
		{
			if (scoreCard[12] == 0)
			{
				candidate[indexPosition] = 12;
				indexPosition++;
			}
		}
		
		return indexPosition;
	}
	
	public int evaluateFullHouse(int [] candidate, int [] scoreCard, int [] results, int currentScore, int indexPosition)
	{
		boolean containsPair = false;
		boolean containsThreeKind = false;
		
		for (int i = 0; i < results.length; i++)
		{
			if (results[i] == 3)
			{
				containsPair = true;
			}
			
			if (results[i] == 2)
			{
				containsThreeKind = true;
			}
		}
		
		if (containsPair == true && containsThreeKind == true)
		{
			if (scoreCard[13] == 0)
			{
				candidate[indexPosition] = 13;
				indexPosition++;
			}
		}
		
		return indexPosition;
	}
	
	public int [] search(String diceResults)
	{
		int [] countTable = new int[7];
		
		for (int i = 0; i < diceResults.length(); i++)
		{
			int value = Integer.parseInt(diceResults.charAt(i) + "");
			
			countTable[value] += 1;
		}
		
		return countTable;
	}
	
	public boolean isSolution(int k, int [] solutionArray, int [] scoreCard, int currentScore)
	{
		int points = 0;
		
		for (int i = 1; i < solutionArray.length; i++)
		{
			if (solutionArray[i] == -1)
			{
				return false;
			}
		}
		
		points = calculateScore(solutionArray, scoreCard);
		
		return (points >= currentScore);
	}
	
	public int calculateScore(int [] solutionArray, int [] scoreCard)
	{
		int points = 0;
		int cardType = 0;
		
		for (int i = 1; i < solutionArray.length; i++)
		{
			cardType = solutionArray[i];
			
			points += scoreCard[cardType];
		}
		
		return points;
	}
	
	public void storeResults(int [] solutionArray, int [] scoreCard, ArrayList<String> tracker, int totalScore)
	{
		int bonus = 0;
		String result = "";
		
		// Check if the sum of the first six categories >= 63.
		if (checkForBonus(scoreCard))
		{
			// 35 bonus points.
			bonus = 35;
		}
		
		for (int i = 1; i < scoreCard.length; i++)
		{
			result += scoreCard[i] + " ";
		}
		
		tracker.add(result.trim() + " " + bonus + " " + totalScore);
	}
	
	public boolean checkForBonus(int [] scoreCard)
	{
		int sum = 0;
		int cardType = 0;
		
		for (int i = 1; i < 6; i++)
		{
			sum += scoreCard[i];
			
			if (sum >= 63)
			{
				return true;
			}
		}
		
		return false;
	}
	
	public String sortString(String arg)
	{
		char [] stringToChar = arg.toCharArray();
		
		Arrays.sort(stringToChar);
		
		String sorted = String.valueOf(stringToChar);
		
		return sorted.trim();
	}
}
